// 档案补录信息管理 Composable

import { ref, computed } from "vue";
import HrApi from "../../api/index";
import type {
  ISysPersonRepairBill,
  ISysOrg,
  ISysGroup,
  ISysJobPosition
} from "../../api/interface";

export function useRepairBillInfo() {
  // API实例
  const api = HrApi();

  // 基础状态
  const loading = ref(false);
  const originalData = ref<ISysPersonRepairBill | null>(null);

  // 选项数据
  const orgOptions = ref<ISysOrg[]>([]);
  const groupOptions = ref<ISysGroup[]>([]);
  const jobPositionOptions = ref<ISysJobPosition[]>([]);





  // 字段映射配置
  const fieldMapping = computed(() => ({
    // 基本信息映射
    "Name": "Name",
    "StaffNo": "StaffNo",
    "IdCardNo": "IdCardNo",
    "Gender": "Gender",
    "BirthDate": "BirthDate",
    "Age": "Age",
    "Ethnicity": "Ethnicity",
    "HighestEducation": "HighestEducation",
    "MajorCategory": "MajorCategory",
    "MajorName": "MajorName",
    "MaritalStatus": "MaritalStatus",
    "FertilityStatus": "FertilityStatus",
    "BloodType": "BloodType",
    "IdCardExpiryDate": "IdCardExpiryDate",

    // 照片映射
    "PhotoId": "PhotoId",
    "IdCardFrontPhotoId": "IdCardFrontPhotoId",
    "IdCardBackPhotoId": "IdCardBackPhotoId",
    "VeteranCertificateId": "VeteranCertificateId",

    // 联系方式映射
    "Phone": "Phone",
    "Email": "Email",
    "HomeAddress": "HomeAddress",

    // 户籍信息映射
    "HouseholdRegistration": "HouseholdRegistration",
    "NativePlace": "NativePlace",

    // 政治信息映射
    "PoliticalStatus": "PoliticalStatus",
    "PartyJoinDate": "PartyJoinDate",
    "PartyBranch": "PartyBranch",
    "ArchiveLocation": "ArchiveLocation",
    "ArchiveNo": "ArchiveNo",
    "ArchiveTransferOutDate": "ArchiveTransferOutDate",
    "ArchiveTransferInDate": "ArchiveTransferInDate",
    "ArchiveAgeDate": "ArchiveAgeDate",
    "IsVeteran": "IsVeteran",

    // 银行信息映射
    "SalaryBank": "SalaryBank",
    "SalaryAccount": "SalaryAccount",
    "BonusBank": "BonusBank",
    "BonusAccount": "BonusAccount",

    // 工作信息映射
    "Org.Name": "Org.Name",
    "Group.GroupName": "Group.GroupName",
    "JobPosition.Name": "JobPosition.Name",
    "JoinWorkDate": "JoinWorkDate",
    "WorkYears": "JoinWorkDate", // 使用参加工作时间来计算工龄
    "HireDate": "HireDate",
    "CompanyYears": "HireDate", // 使用入职日期来计算司龄
    "ConfirmationDate": "ConfirmationDate",
    "EmploymentType": "EmploymentType",
    "ContractType": "ContractType",
    "ContractNo": "ContractNo",
    "ContractStartDate": "ContractStartDate",
    "ContractEndDate": "ContractEndDate",

    // 其他信息映射
    "Hobbies": "Hobbies",
    "Remark": "Remark"
  }));

  // 获取图片URL
  const getImageUrl = (fileId: string | null): string => {
    if (!fileId) return "";
    return `${import.meta.env.VITE_BASE_URL}/api/_file/DownloadFile/${fileId}`;
  };

  // 初始化选项数据
  const initializeOptions = async () => {
    try {
      const [orgs, groups, positions] = await Promise.all([
        api.getOrgList(),
        api.getGroupList(),
        api.getJobPositionList()
      ]);

      orgOptions.value = orgs || [];
      groupOptions.value = groups || [];
      jobPositionOptions.value = positions || [];
    } catch (error) {
      console.error("加载选项数据失败:", error);
    }
  };

  // 加载档案补录详情
  const loadRepairBillDetail = async (id: string) => {
    loading.value = true;
    try {
      const response = await api.getRepairBill(id);
      if (response && response.Entity) {
        originalData.value = response.Entity;
        return {
          success: true,
          isMyApprove: response.IsMyApprove || false
        };
      } else {
        return { success: false, isMyApprove: false };
      }
    } catch (error) {
      console.error("加载档案补录详情失败:", error);
      return { success: false, isMyApprove: false };
    } finally {
      loading.value = false;
    }
  };



  return {
    // 状态
    loading,
    originalData,

    // 选项数据
    orgOptions,
    groupOptions,
    jobPositionOptions,

    // 计算属性
    fieldMapping,

    // 方法
    getImageUrl,
    initializeOptions,
    loadRepairBillDetail,
  };
}
